package com.bw.mallshop.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.bw.mallshop.R;
import com.bw.mallshop.entity.HomeMenuEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

public class HomeMenuAdapter extends BaseQuickAdapter<HomeMenuEntity, BaseViewHolder> implements OnItemClickListener {
    public HomeMenuAdapter() {
        super(R.layout.item_home_menu);
        setOnItemClickListener(this);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder,
                           HomeMenuEntity homeMenuEntity) {
        baseViewHolder.setImageResource(R.id.home_menu_img,homeMenuEntity.iconId);
        baseViewHolder.setText(R.id.home_menu_tv,homeMenuEntity.title);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        String pagePath = getItem(position).pagePath;
        if (TextUtils.isEmpty(pagePath))
            return;
        try {
            Class clazz = Class.forName(pagePath);
            Intent intent = new Intent(getContext(),clazz);
            AppCompatActivity appCompatActivity = (AppCompatActivity) getContext();
            appCompatActivity.startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
